package com.bkfm.framework.core.utils;

import org.apache.commons.lang3.StringUtils;
import org.yak.core.utils.JsonUtils;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BkfmUtils {

	/**
	 * 格式化下拉选项
	 * @param selOptions
	 * @param tempTableName 子查询表别名
	 * @return
	 */
	public static String parseSelOptions(String selOptions){
		if(StringUtils.isEmpty(selOptions)){
			return "";
		}

		JSONObject selJson = JSONObject.fromObject(selOptions).getJSONObject("RangConfig");
		int configType = selJson.getInt("configType");
		StringBuffer childSql = new StringBuffer();
		//子查询表别名

		switch(configType){
			case 0:{
				break;
			}
			case 1:{
				JSONArray configArray = selJson.getJSONArray("Config");
				for(int i=0;i<configArray.size();i++){
					JSONObject obj = configArray.getJSONObject(i);
					String key = JsonUtils.getSingJsonKey(obj);
					String value = obj.getString(key);
					if(i==0){
						childSql.append("SELECT '").append(key).append("' AS VALUE,'").append(value).append("' AS DISPLAYVALUE");
					}else{
						childSql.append(" UNION ")
						.append("SELECT '").append(key).append("' AS VALUE,'").append(value).append("' AS DISPLAYVALUE");
					}
				}
				break;
			}
			case 2:{
				JSONObject sqlConfig = selJson.getJSONObject("Config");
				childSql.append(sqlConfig.get("Sql"));
				break;
			}
		}
		return childSql.toString();
	}
	
}
